package edu.fudan.jwxt.service.Impl;

import cn.hutool.core.lang.Pair;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.fudan.jwxt.dao.CourseTimeVODao;
import edu.fudan.jwxt.entity.VO.CourseTimeVO;
import edu.fudan.jwxt.service.CourseTimeVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseTimeVOServiceImpl extends ServiceImpl<CourseTimeVODao, CourseTimeVO> implements CourseTimeVOService {

    @Autowired
    private CourseTimeVODao courseTimeVODao;

    @Override
    public List<Pair<Integer, Integer>> getTimeByCourse(String id) {
        List<Pair<Integer,Integer>> result = new ArrayList<>();
        LambdaQueryWrapper<CourseTimeVO> q = new LambdaQueryWrapper<>();
        q.eq(CourseTimeVO::getCourseId,id);
        List<CourseTimeVO> list = courseTimeVODao.selectList(q);


        for(CourseTimeVO c : list){
            Pair<Integer,Integer> pair = new Pair<>(c.getWeekDay(),c.getTimeId());
            result.add(pair);
        }

        return result;
    }

    @Override
    public void saveTimeByCourse(String courseId,List<Pair<Integer, Integer>> list) {

        LambdaQueryWrapper<CourseTimeVO> q = new LambdaQueryWrapper<>();
        q.eq(CourseTimeVO::getCourseId,courseId);
        this.remove(q);

        for(Pair<Integer, Integer> p : list){
            CourseTimeVO courseTimeVO = new CourseTimeVO();
            courseTimeVO.setCourseId(courseId);
            courseTimeVO.setWeekDay(p.getKey());
            courseTimeVO.setTimeId(p.getValue());
            this.save(courseTimeVO);
        }
    }
}
